package com.web;

import com.pojo.user;
import com.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(value = "/servletRegister")
public class servletRegister extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取注册的用户名密码
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String check = req.getParameter("check");//验证码

        HttpSession session = req.getSession();     //获取程序生成的验证码
        String check1 = (String)session.getAttribute("check");

        if(!check1.equalsIgnoreCase(check))      //忽略大小写比对
        {
            //不一样
            req.setAttribute("rig_msg","验证码错误");
            req.getRequestDispatcher("/register.jsp").forward(req,res);
            return;
        }

        UserService us = new UserService();
        boolean a = us.add(name, password);//调用方法注册

        if(a)   //a为true，注册成功
        {
            req.setAttribute("rig_msg","注册成功，请登录");
            req.getRequestDispatcher("/login.jsp").forward(req,res);
        }else{
            req.setAttribute("rig_msg","用户名已存在");
            req.getRequestDispatcher("/register.jsp").forward(req,res);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
